package com.caigou.controller.front;

import com.alibaba.fastjson.JSONObject;
import com.caigou.bean.cms.param.CollectionLikesParam;
import com.caigou.bean.exam.entity.Users;
import com.caigou.bean.vo.ArticleCollectionLikesVO;
import com.caigou.bean.vo.CollectionLikesVO;
import com.caigou.enums.Constant;
import com.caigou.enums.FailResultEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.ICollectionLikesService;
import com.caigou.service.IUsersService;
import com.caigou.util.Result;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.List;

@RestController
@RequestMapping("/front/article")
public class FrontCollectionLikesController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUsersService usersService;
    @Resource
    private ICollectionLikesService collectionLikesService;

    @PostMapping("/collection")
    public Result<ArticleCollectionLikesVO> collectionArticle(@RequestBody CollectionLikesParam param, @CurrentUser Users user) {
        collectionLikesService.isCollectionArticle(param, user.getUserid(), true);

        return collectionLikesService.findArticleCollectionLikes(param, user);
    }

    @PostMapping("/unCollection")
    public Result<ArticleCollectionLikesVO> unCollectionArticle(@RequestBody CollectionLikesParam param, @CurrentUser Users user) {
        collectionLikesService.isCollectionArticle(param, user.getUserid(), false);
        return collectionLikesService.findArticleCollectionLikes(param, user);
    }

    @PostMapping("/like")
    public Result<ArticleCollectionLikesVO> likeArticle(@RequestBody CollectionLikesParam param, @CurrentUser Users user) {
        collectionLikesService.isLikeArticle(param, user.getUserid(), true);
        return collectionLikesService.findArticleCollectionLikes(param, user);
    }

    @PostMapping("/unLike")
    public Result<ArticleCollectionLikesVO> unLikeArticle(@RequestBody CollectionLikesParam param, @CurrentUser Users user) {
        collectionLikesService.isLikeArticle(param, user.getUserid(), false);
        return collectionLikesService.findArticleCollectionLikes(param, user);
    }

    @PostMapping("/collectionLikes")
    public Result<ArticleCollectionLikesVO> articleCollectionLikes(@RequestBody CollectionLikesParam param) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        Users user = usersService.findUserByTel(auth.getName());
        return collectionLikesService.findArticleCollectionLikes(param, user);
    }


}
